package com.coco.group.controller;

import com.coco.common.annotation.IngoreResponseAdvice;
import com.coco.common.result.ResultData;
import com.coco.group.pojo.TbGroupBuyGoods;
import com.coco.group.service.GroupBuyGoodsService;
import com.coco.group.vo.GroupGoodsAndItemVo;
import com.coco.group.vo.GroupGoodsSearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Package: com.coco.group.controller
 * @Author: 张dy
 * @Date: Created in 2021/7/29 下午 7:57
 * 团购spu商品
 */
@RestController
@RequestMapping("/group/goods")
public class GroupBuyGoodsController {

    @Autowired
    private GroupBuyGoodsService groupBuyGoodsService;

    /**
     * 添加拼团商品
     * @Author: Lin
     * @param groupGoodsAndItemVo
     */
    @PostMapping("/addGroupGoods")
    public void addGroupGoods(@RequestBody GroupGoodsAndItemVo groupGoodsAndItemVo){
        try {
            groupBuyGoodsService.addGroupGoods(groupGoodsAndItemVo);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 查询单条
     * @Author: Lin
     * @param id
     * @return GroupGoodsAndItemVo
     */
    @GetMapping("/findOne/{id}")
    public GroupGoodsAndItemVo findOne(@PathVariable("id") Long id){
        return groupBuyGoodsService.findOne(id);
    }

    /**
     * 修改拼团商品信息
     * @Author: 张dy
     * @Date: 2021/7/30
     * @Param:
     */
    @PutMapping("/updateGroupGoods")
    private void updateGroupGoods(@RequestBody GroupGoodsAndItemVo goods) {
        try{
            groupBuyGoodsService.updateGroupGoods(goods);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 查询spu信息
     * @param id
     * @return
     */
    @GetMapping("/findGoodsById/{id}")
    @IngoreResponseAdvice
    public TbGroupBuyGoods findGoodsById(@PathVariable("id") Long id){
        return groupBuyGoodsService.findGoodsById(id);
    }
    /**
     * 查询所有的拼团商品
     * @Author: 张dy
     * @Date: 2021/7/29
     */
    @GetMapping("/findAll")
    private List<TbGroupBuyGoods> findAll(){
        return groupBuyGoodsService.findAll();
    }

    /**
     * 分页和模糊
     * @Author: 张dy
     * @Date: 2021/7/29
     */
    @PostMapping("/search")
    private ResultData<TbGroupBuyGoods> search(@RequestBody GroupGoodsSearchVo groupGoodsSearchVo){
        return groupBuyGoodsService.search(groupGoodsSearchVo);
    }

    /**
     * 修改拼团商品状态
     * @Author: 张dy
     * @Date: 2021/7/30
     * @Param:
     */
    @PutMapping("/updateStatus/{id}/{status}")
    private void updateStatus(@PathVariable("id") Long id,
                              @PathVariable("status") String status) {
        try{
            groupBuyGoodsService.updateStatus(id,status);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 逻辑删除
     * @Author: 张dy
     * @Date: 2021/7/30
     * @Param:
     */
    @DeleteMapping("/deleteGroupGoods/{id}")
    private void deleteGroupGoods(@PathVariable("id") Long id) {
        try{
            groupBuyGoodsService.deleteGroupGoods(id);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 修改spu的参与人数和库存
     * @param gid
     */
    @GetMapping("/updateJoinNumAndStock/{gid}")
    @IngoreResponseAdvice
    public void updateJoinNumAndStock(@PathVariable("gid") Long gid){
        groupBuyGoodsService.updateJoinNumAndStock(gid);
    }
    /**
     * 添加spu的限量剩余库存
     */
    @GetMapping("/addStock/{gid}/{num}")
    @IngoreResponseAdvice
    public void addStock(@PathVariable("gid") Long gid,@PathVariable("num") Integer num){
        groupBuyGoodsService.addStock(gid,num);
    }
}
